﻿using System;
using System.Collections.Generic;
using System.Text;
using RS.DOM.Common;
using RS.DOM.Construction;

namespace RS.DOM.Types
{
    public class Float: Token
    {
        #region Constructors

        public Float(Context context)
            : base(context)
        {
            base.TokenType = TokenType.Float;
        }

        public Float(Context context, object result)
            :base(context, result)
        {
            base.TokenType = TokenType.Float;
        }

        #endregion

        #region Methods

        public override Token Operation(Token token, OperationType type)
        {
            Token result = this;

            if(token.TokenType == TokenType.Number)
            {
                result = new Float(this.Context);
                if (type == OperationType.Plus)
                {
                    result.Result = (float)this.Result + Convert.ToDouble(token.Result);
                }
                else if (type == OperationType.Subtract)
                {
                    result.Result = (float)this.Result - Convert.ToDouble(token.Result);
                }
                else if (type == OperationType.Multiply)
                {
                    result.Result = (float)this.Result * Convert.ToDouble(token.Result);
                }
                else if (type == OperationType.Devide)
                {
                    result.Result = (float)this.Result / Convert.ToDouble(token.Result);
                }
            }
            else if (token.TokenType == TokenType.Float)
            {
                result = new Float(this.Context);
                if (type == OperationType.Plus)
                {
                    result.Result = (float)this.Result + (float)token.Result;
                }
                else if (type == OperationType.Subtract)
                {
                    result.Result = (float)this.Result - (float)token.Result;
                }
                else if (type == OperationType.Multiply)
                {
                    result.Result = (float)this.Result * (float)token.Result;
                }
                else if (type == OperationType.Devide)
                {
                    result.Result = (float)this.Result / (float)token.Result;
                }
            }
            else
            {
                base.Operation(token, type);
            }

            return result;
        }

        public override Token Create()
        {
            return new Float(this.Context, this.Result);
        }

        public float Result
        {
            get
            {
                return (float)base.Result;
            }

            set
            {
                base.Result = value;
            }
        }

        #endregion
    }
}
